//
//  PicturePiickerViewController.swift
//  CJPWB
//
//  Created by 陈锦彭 on 16/3/22.
//  Copyright © 2016年 chenjinpeng. All rights reserved.
//

import UIKit

private let picturePickerViewCellID = "picturePickerViewCellID"

class PicturePickerViewController: UICollectionViewController {
     // MARK: - 属性
    ///存放图片的数据
    lazy var pictureArray : [UIImage] = [UIImage]()
    override func viewDidLoad() {
        super.viewDidLoad()
    ///注册cell
        self.collectionView?.registerNib(UINib(nibName: "PictureCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: picturePickerViewCellID)
     

    }

}

// MARK: - 数据源方法
extension PicturePickerViewController {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureArray.count + 1
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
       //1.创建cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(picturePickerViewCellID, forIndexPath: indexPath) as! PictureCollectionViewCell
        //2.给cell设置数据
        cell.image =  indexPath.item <= pictureArray.count - 1 ? pictureArray[indexPath.item] : nil
        cell.delegate = self
     
        //3.返回cell
        return cell
        
    }

}


// MARK: - cell的代理方法
extension PicturePickerViewController : PictureCollectionViewCellDelegate {

    func pictureCollectionViewCellAddPictureBtnClick(cell: PictureCollectionViewCell) {
        //1.判断照片源是否可用
        guard UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) else {
        return
        }
        //2.创建照片选择控制器
       let pickerVC = UIImagePickerController()
        //3.设置照片源
        pickerVC.sourceType = .PhotoLibrary
        //4.设置代理
        pickerVC.delegate = self
        //5.弹出控制器
        presentViewController(pickerVC, animated: true, completion: nil)
        
    }
    //删除照片
    func pictureCollectionViewCellRemovePictureBtnClick(cell: PictureCollectionViewCell) {
        guard let indexPath = collectionView?.indexPathForCell(cell) else {
        return
        }
         pictureArray.removeAtIndex(indexPath.item)
        collectionView?.reloadData()
    }
}


// MARK: - 照片选择控制器的打理方法
extension PicturePickerViewController : UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    //选择完图片来到这个方法
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        guard let image = info["UIImagePickerControllerOriginalImage"] else {
        return
        }
        pictureArray.append(image as! UIImage)
        collectionView?.reloadData()
        dismissViewControllerAnimated(true, completion: nil)
    }
}

// MARK: - 流水布局
class PicturePickerViewLayout : UICollectionViewFlowLayout {

    override func prepareLayout() {
        let itemWH : CGFloat = 100
       let cols : CGFloat = 3
       let  cellMargin = (screenW - cols * itemWH) / (cols + 2)
      itemSize = CGSizeMake(itemWH, itemWH)
        minimumInteritemSpacing = cellMargin
        minimumLineSpacing = cellMargin
        sectionInset = UIEdgeInsetsMake(cellMargin, cellMargin, cellMargin, cellMargin)
    }


}